package org.xhy.hrm.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.xhy.hrm.constants.MQConstants;

/**
 * 消息队列配置
 */
@Configuration
public class MQConfig {

    @Bean
    public Exchange killExchange(){
        return ExchangeBuilder.topicExchange(MQConstants.NAME_EXCHANGE_KILL_TOPIC).build();
    }

    @Bean
    public Queue queue(){
        return new Queue(MQConstants.NAME_QUEUE_KILL_ORDER);
    }

    @Bean
    public Binding bindingOrderQueue(){
        return BindingBuilder.bind(queue()).to(killExchange()).with("killcourse.order").noargs();
    }

    //自定义RabbitTemplate
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        //如果消息发送到MQ失败，把消息返回回来
        rabbitTemplate.setMandatory(true);
        return rabbitTemplate;
    }

}
